﻿namespace CKFinder.Connector
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;

    internal class Lang
    {
        internal const string LANG_PATH = "lang";

        public static string getErrorMessage(int number)
        {
            string input = HttpContext.Current.Request.QueryString["langCode"];
            if ((input == null) || !Regex.IsMatch(input, @"^[a-z\-]+$"))
            {
                input = "en";
            }
            if (!File.Exists(HttpContext.Current.Server.MapPath(Path.Combine("lang", input + ".xml"))))
            {
                input = "en";
            }
            string xml = Util.ReadTextFile(HttpContext.Current.Server.MapPath(Path.Combine("lang", input + ".xml")));
            XmlDocument parentNode = new XmlDocument();
            parentNode.LoadXml(xml);
            try
            {
                return XmlUtil.GetNodeValue(parentNode, "messages/errors/error[@number='" + number.ToString() + "']", "");
            }
            catch (Exception)
            {
                return XmlUtil.GetNodeValue(parentNode, "messages/errorUnknown", "");
            }
        }
    }
}

